<?php
define('IN_TG','member_friend');
define('IN_JS','member_message');
define('SCRIPT','member_friend');
session_start();
require('includes/common.inc.php');
//加载头部文件
require('header.php');
//分页计算
$size = 10;
$sql = "select tg_id,tg_fromuser,tg_content,tg_state,tg_date from tg_friend where tg_touser ='{$_SESSION['username']}'";
_page($sql,$size);
$sql .= " order by tg_id limit $_pagenum,$size";
$res = _query($sql);
$friend_arr = array();
while ($row = _fetch_array_list($res)) {
    $friend_arr[] = $row;
}
if(!empty($_POST))
{
	if($_GET['action']=='delete')
	{
		if(isset($_POST['delete_ids']))
		{
            if (isset($_POST['ids']) && !empty($_POST['ids']) > 0) {
                $ids = implode(',', $_POST['ids']);
                $sql = "delete from tg_friend where tg_id in ({$ids})";
                _query($sql);
            } else {
                _alert_back('请进行选择，再做进一步操作');
            }
		}
		else if(isset($_POST['state_ids']))
		{
            if (isset($_POST['ids']) && !empty($_POST['ids']) > 0) {
                $ids = implode(',', $_POST['ids']);
                $sql = "update tg_friend set tg_state=1 where tg_id in ({$ids})";
                _query($sql);
            } else {
                _alert_back('请进行选择，再做进一步操作');
            }
		}
		header("location:member_friend.php");
	}
}
?>
<div id="member">
	<?php
		require ("includes/member.inc.php");
	?>
	<div id="member_main">
		<h2>好友设置</h2>
        <?php if (count($friend_arr) > 0): ?>
		<form method="post" action="?action=delete">
		<table cellspacing="1">
			<tr>
				<th>好友</th>
				<th>请求内容</th>
				<th>时间</th>
				<th>状态</th>
				<th>操作</th>
			</tr>
            <?php foreach ($friend_arr as $row): ?>
                <tr>
                    <td><?php echo $row['tg_fromuser'] ?></td>
                    <td title="<?php echo $row['tg_content'] ?>"><?php echo $row['tg_content'] ?></td>
                    <td><?php echo $row['tg_date'] ?></td>
                    <td><span style="color:blue;"><?php echo $row['tg_state'] == 1 ? "已验证" : "未验证"; ?></span></td>
                    <td><input name="ids[]" value="<?php echo $row['tg_id'] ?>" type="checkbox" class="ids"/></td>
                </tr>
            <?php endforeach; ?>
            <tr>
                <td colspan="5">
                    <label for="all">全选 <input type="checkbox" name="chkall" id="all"/></label>
                    <input type="submit" value="批删除" name="delete_ids" id="delete_ids"/>
                    <input type="submit" value="批验证" name="state_ids" id="state_ids"/>
                </td>
            </tr>
		</table>
		</form>
		<div id="page_text"><?php _paging(2)?></div>
        <?php else: ?>
            <h3 style="text-align:center;margin-top:20px;">暂无相关数据</h3>
        <?php endif ?>
	</div>
</div>

<?php
require('includes/footer.inc.php');
?>